package com.ymatou.database.wrapper;

import com.ymatou.database.model.Account;
import com.ymatou.database.model.AccountEntry;
import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AccountWapper {
    SqlManager manage;

    public AccountWapper() {
        manage = new SqlManager("accounting");
    }

    /**
     * 使用 accountid 查询信息
     *
     * @param accountid
     * @return {dCreateTime=2016-01-26 10:33:57.263, dUpdateTime=2016-01-26
     *         10:33:57.263, fAvailAmount=0.00, iAction=0, iCurrencyType=1,
     *         iUserId=20000000, iAccountType=0, Version=0, fRewardAmount=0.00,
     *         fFreezeAmount=0.00, sAccountInfoId=00010020000000, sMemo=自动化测试专用}
     * @throws InterruptedException
     */
    public List<Account> selectAccountInfo(String accountid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        List<Account> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountInfo", accountid);
        manage.commit();
        return pia;
    }

    /**
     * 使用 userId, accountType查询账户信息
     *
     * @param userId
     *            用户编号
     * @param accountType
     *            账户类型
     * @return {dCreateTime=2016-01-26 10:33:57.263, dUpdateTime=2016-01-26
     *         10:33:57.263, fAvailAmount=0.00, iAction=0, iCurrencyType=1,
     *         iUserId=20000000, iAccountType=0, Version=0, fRewardAmount=0.00,
     *         fFreezeAmount=0.00, sAccountInfoId=00010020000000, sMemo=自动化测试专用}
     * @throws InterruptedException
     */
    public List<Account> selectAccountInfoByUserId(int userId, int accountType) throws InterruptedException {
        Map m = new HashMap<String, Integer>();
        m.put("userId", userId);
        m.put("accountType", accountType);
        List<Account> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountInfoByUserId", m);
        manage.commit();
        return pia;
    }

    /**
     * 使用 userId 查询账户信息
     *
     * @param userId
     * @return {dCreateTime=2016-01-26 10:33:57.263, dUpdateTime=2016-01-26
     *         10:33:57.263, fAvailAmount=0.00, iAction=0, iCurrencyType=1,
     *         iUserId=20000000, iAccountType=0, Version=0, fRewardAmount=0.00,
     *         fFreezeAmount=0.00, sAccountInfoId=00010020000000, sMemo=自动化测试专用}
     * @throws InterruptedException
     */
    public List<Account> selectAccountInfoByUserId(int userId) throws InterruptedException {
        //支付异步调用所以需要等待
        Thread.sleep(1000);
        Map m = new HashMap<String, Integer>();
        m.put("userId", userId);
        List<Account> pia = null;
        try {
            pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountInfoByUserId", m);
            manage.commit();
        } catch (Throwable e) {
            if (pia == null || pia.size() <= 0) {
                Thread.sleep(1000);
                pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountInfoByUserId", m);
                manage.commit();
            }
        }
        // manage.commit();
        return pia;
    }

    /**
     * 使用 accountid 查询信息
     *
     * @param accountid
     * @param ono
     * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
     *         Operator=AutoTestOperator,
     *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
     *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
     *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
     *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
     *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
     *         ServerIp=172.16.100.175,
     *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
     *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
     *         ClientIp=172.16.13.71} * @throws InterruptedException
     */
    public List<AccountEntry> selectAccountEntry(String accountid, String ono) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap<String, String>();
        m.put("accountId", accountid);
        m.put("oNo", ono);
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.mapping.AccountMapper.selectAccountEntry", m);
        manage.commit();
        return pia;
    }

    /**
     * 动态关联Ymt_AccountEntry ,ymt_accountinfo查询账户流水信息
     *
     * @param m
     * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
     *         Operator=AutoTestOperator,
     *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
     *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
     *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
     *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
     *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
     *         ServerIp=172.16.100.175,
     *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
     *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
     *         ClientIp=172.16.13.71} * @throws InterruptedException
     */
    public List<AccountEntry> selectAccountEntryAndInfo(Map m) throws InterruptedException {
        @SuppressWarnings("unchecked")
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryAndInfo2", m);
        manage.commit();
        return pia;
    }

    /**
     * 重载方法 使用 accountid 查询信息
     *
     * @param accountid
     * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
     *         Operator=AutoTestOperator,
     *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
     *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
     *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
     *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
     *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
     *         ServerIp=172.16.100.175,
     *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
     *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
     *         ClientIp=172.16.13.71} * @throws InterruptedException
     */
    public List<AccountEntry> selectAccountEntry(String accountid) throws InterruptedException {
        @SuppressWarnings("unchecked")
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryByAcId", accountid);
        manage.commit();
        return pia;
    }

    /**
     * 重载方法 使用 userid, bizNo 查询信息
     *
     * @param userId
     * @param bizNo
     * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
     *         Operator=AutoTestOperator,
     *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
     *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
     *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
     *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
     *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
     *         ServerIp=172.16.100.175,
     *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
     *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
     *         ClientIp=172.16.13.71} * @throws InterruptedException
     */
    public List<AccountEntry> selectAccountEntry(int userId, String bizNo, int accountType) throws InterruptedException {
        @SuppressWarnings("unchecked")
        Map m = new HashMap();
        m.put("userId", userId);
        m.put("bizNo", bizNo);
        m.put("accountType", accountType);
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryByUserid", m);
        manage.commit();
        return pia;
    }

    /**
     * 关联查询Ymt_AccountEntry,Ymt_AccountInfo2张表 返回前10条记录
     *
     * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
     *         Operator=AutoTestOperator,
     *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
     *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
     *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
     *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
     *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
     *         ServerIp=172.16.100.175,
     *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
     *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
     *         ClientIp=172.16.13.71} * @throws InterruptedException
     *         ??????????? 需要确认返回字段
     */
    public List<HashMap> selectAccountEntryAndInfo() throws InterruptedException {
        @SuppressWarnings("unchecked")
        List<HashMap> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryAndInfo", null);
        return pia;
    }

    /**
     * 根据userid查询Ymt_AccountEntry，返回updatetime 最新的一条数据
     */
    public List<AccountEntry> selectAccountEntryByUserId(int userId) throws InterruptedException {
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryByUserid2", userId);
        manage.commit();
        return pia;
    }

    public int selectCountByUserid(int userId) throws InterruptedException {
        List<Map> m = (List<Map>) manage.getSelectResults("com.ymatou.database.mapper.mapping.AccountMapper.selectCountByUserid", userId);
        manage.commit();
        int pia = Integer.valueOf (m.get(0).get("count").toString());
        return pia;
    }

    /**
     * 根据userid查询Ymt_AccountEntry，返回updatetime 最新limit条数据
     */
    public List<AccountEntry> selectAccountEntryByUseridLimit(String userId, int limit) throws InterruptedException {
        Map m = new HashMap();
        m.put("userId", userId);
        m.put("limit", limit);
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryByUseridLimit", m);
        return pia;
    }

    /**
     * 根据userid查询Ymt_AccountEntry，返回updatetime 最新limit条数据
     */
    public List<AccountEntry> selectAccountEntryByBizNo(Map m) throws InterruptedException {
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryByBizNo", m);
        manage.commit();
        return pia;
    }

    /**
     * 根据originalNo
     */
    public List<AccountEntry> selectAccountEntryByOriginalNo(String originalNo) throws InterruptedException {
        Thread.sleep(1000);
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryByOriginalNo", originalNo);
        manage.commit();
        return pia;
    }
    public List<AccountEntry> selectAccountEntryByMap(Map m) {
        List<AccountEntry> pia = manage.getSelectResults("com.ymatou.database.mapper.AccountMapper.selectAccountEntryByMap", m);
        manage.commit();
        return pia;
    }
    /**
     * 关闭数据库连接
     */
    public void close() {
        manage.close();
    }

    @Override
    protected void finalize() throws Throwable {
        this.close();
        manage = null;
        super.finalize();
    }

    public static void main(String args[]) {
        // List<AccountEntry> m=new
        // AccountDefaultWapper().selectAccountEntry(355976173,"f7a44e0ae1464e3391e46e67e608cfd5",0);
        // List<Map> m=new
        // AccountDefaultWapper().selectAccountInfoByUserId(20000002,0);
        HashMap<String, Object> accountEntryMap = new HashMap<>();

        accountEntryMap.put("userId", 3002);
        accountEntryMap.put("bizcode", "300046");
        accountEntryMap.put("originalNo", "TK07942598699");
        /*
         * List<HashMap> rm=new
         * AccountDefaultWapper().selectAccountEntryByMap(m);
         */

        List<AccountEntry> rm = new AccountWapper().selectAccountEntryByMap(accountEntryMap);

        Logger.debug("msize:" + rm.get(0).getAvailAmount());
        // Logger.debug("m:"+FormatUtil.beanToGSONString(rm));
    }
}
